package handler

import (
	"ginweb/module/db/model"
	"ginweb/module/db/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

// FindUserByID 根据用户Id查询用户
func FindUserByID(c *gin.Context) {
	id, pErr := strconv.Atoi(c.Param("id"))
	if pErr != nil || id < 0 {
		c.JSON(500, "param error")
		return
	}

	c.JSON(200, service.FindUserByID(id))
}

// FindAllUsers 查询所有用户
func FindAllUsers(c *gin.Context) {
	c.JSON(200, service.FindAllUsers())
}

// SaveUser 新增或修改用户
func SaveUser(c *gin.Context) {
	var user model.SysUser
	if c.ShouldBind(&user) == nil {
		if user.Account == "" || user.Name == "" {
			c.JSON(500, "param error")
			return
		}

		err := service.SaveUser(&user)
		if err != nil {
			c.JSON(500, "user save or update fail")
			return
		}

		c.JSON(200, "success")

	} else {
		c.JSON(500, "param error")
		return
	}
}
